package cn.cnf.common.utils;

import org.apache.commons.lang3.StringUtils;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;

/**
 * @author ZhuMeiYi
 * @date 2020/12/24 16:58
 * @description
 */
public class UserRequestUtil {

    public static String getCurrentToken() {
        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes())
                .getRequest();
        String authorization = request.getHeader("Authorization");
        if (StringUtils.isBlank(authorization)) {
            return null;
        }
        return authorization.replace(JwtTokenUtil.TOKEN_PREFIX, "");
    }

    public static String getCurrentUserName() {
        return JwtTokenUtil.getUsername(getCurrentToken());
    }

    public static String getCurrentUserRole() {
        return JwtTokenUtil.getUserRole(getCurrentToken());
    }
}
